package ro.xblue.translator;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: ShowMessageTag.java,v 1.1 2004/10/24 23:37:03 mihaipostelnicu Exp $
 */
public class ShowMessageTag extends TagSupport
{
      private String module;
      private String lang;
      private String label;

      public void setLabel( String label )
      {
            this.label = label;
      }

      public String getLabel()
      {
            return label;
      }

      public void setModule( String module )
      {
            this.module = module;
      }

      public String getModule()
      {
            return module;
      }

      public void setLang( String lang )
      {
            this.lang = lang;
      }

      public String getLang()
      {
            return lang;
      }

      public int doEndTag() throws JspException
      {
            if ( ( label == null ) || ( module == null ) )
            {
                  return EVAL_PAGE;
            }

            if ( lang == null )
            {
                  LanguageBean bean = ( LanguageBean ) pageContext.getSession().
                        getAttribute( TranslatorConstants.BLUE_CLANG );
                  if ( bean == null )
                  {
                        lang = "ro";
                        //TODO:this should be dynamic!
                  }
                  else
                  {
                        lang = bean.language;
                  }
            }

            String message = null;

            Translator_DB translator_db = ( Translator_DB ) pageContext.getServletContext().
                  getAttribute( TranslatorConstants.DB_TRANSLATOR );
            if ( translator_db != null )
            {
                  message = translator_db.getMessage( lang, module, label );
            }
            else
            {
//                  Translator translator = ( Translator ) pageContext.getServletContext().
//                        getAttribute( TranslatorConstants.BLUE_TRANSLATOR );
//                  message = translator != null ?
//                        translator.getMessage( lang, module, label ) :
//                        null;
                  message = "N/A";
            }


            if ( message == null )
            {
                  message = label;
            }

            JspWriter writer = pageContext.getOut();
            try
            {
                  writer.print( message );
            }
            catch ( IOException ioEx )
            {
                  System.err.println( ioEx );
            }

            this.lang = null;
            this.label = null;
            this.module = null;

            return EVAL_PAGE;
      }

      public static String getMessage(
            ServletContext application,
            String module_name,
            String label,
            String lang )
      {

            String result = null;

//            Translator translator = ( Translator ) application.
//                  getAttribute( TranslatorConstants.BLUE_TRANSLATOR );
            Translator_DB translator_db = ( Translator_DB ) application.
                  getAttribute( TranslatorConstants.DB_TRANSLATOR );
//            if ( translator != null )
//            {
//                  result = translator.getMessage( lang, module_name, label );
//            }
//            else if ( translator_db != null )
//            {
            result = translator_db.getMessage( lang, module_name, label );
//            }

            if ( result == null )
                  result = label;
            return result;
      }

      public int doStartTag() throws JspException
      {
            return SKIP_BODY;
      }
}
